package com.loie.xcx.syUtil;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.io.FileWriter;
import java.io.IOException;

public
class PostmanJsonGenerator {
    public static
    void main(String[] args) {
        // 示例用法
        generateAndWritePostmanCollection("买方代码EDI", "a618b11c-436d-432c-8040-1d56e07ddd13", "postman_collection.json");
    }


    public static
    void generateAndWritePostmanCollection(String collectionName, String postId, String fileName) {
        // 创建 Postman Collection 对象
        JSONObject postmanCollection = createPostmanCollection(collectionName, postId);

        // 将 JSON 对象转换为字符串
        String jsonString = postmanCollection.toJSONString();

        // 打印生成的 JSON 字符串
        System.out.println(jsonString);

        // 将 JSON 字符串写入文件
        writeJsonToFile(jsonString, fileName);
    }

    public static
    JSONObject createPostmanCollection(String collectionName, String postId) {
        // 创建一个 Postman Collection 对象
        JSONObject postmanCollection = new JSONObject();

        // 设置 Collection 的基本信息
        JSONObject info = new JSONObject();
        info.put("_postman_id", postId);
        info.put("name", collectionName);
        info.put("schema", "https://schema.getpostman.com/json/collection/v2.1.0/collection.json");
        postmanCollection.put("info", info);

        // 添加请求到 Collection
        JSONArray items = new JSONArray();
        JSONObject item = new JSONObject();
        item.put("name", "买方代码申请");

        // 添加事件（Event）
        JSONArray events = new JSONArray();
        JSONObject event = new JSONObject();
        event.put("listen", "test");

        // 添加事件脚本（Script）
        JSONObject script = new JSONObject();
        script.put("type", "text/javascript");
        script.put("exec", getTestScriptContent());
        event.put("script", script);

        events.add(event);
        item.put("event", events);

        // 添加请求（Request）
        JSONObject request = new JSONObject();
        request.put("method", "POST");

        // 添加请求头（Headers）
        JSONArray headers = new JSONArray();
        request.put("header", headers);

        // 添加请求体（Body）
        JSONObject body = new JSONObject();
        body.put("mode", "raw");
        body.put("raw", getRequestBodyContent());
        request.put("body", body);

        // 添加请求 URL
        JSONObject url = new JSONObject();
        url.put("raw", "{{host}}/ediserver/gateway.do");
        JSONArray host = new JSONArray();
        host.add("{{host}}");
        url.put("host", host);
        JSONArray path = new JSONArray();
        path.add("ediserver");
        path.add("gateway.do");
        url.put("path", path);
        request.put("url", url);

        item.put("request", request);
        items.add(item);

        postmanCollection.put("item", items);

        return postmanCollection;
    }

    public static
    JSONArray getTestScriptContent() {
        // 返回测试脚本的内容
        JSONArray execArray = new JSONArray();
        execArray.add("//var corpserialnos = JSON.parse(responseBody);\r");
        execArray.add("\r");
        execArray.add("//pm.environment.set(\"corpserialnos\", corpserialnos.data[0].name);\r");
        execArray.add("\r");
        execArray.add("//var jsonDate = pm.response.json();\r");
        execArray.add("var jsonDate = JSON.parse(responseBody);\r");
        execArray.add("pm.environment.set(\"corpserialnos\",jsonDate.data.corpserialno);\r");
        execArray.add("//console.log(jsonDate.data.corpserialno);\r");
        execArray.add("var corpserialnos = pm.environment.get(\"corpserialnos\");\r");
        execArray.add("//console.log(corpserialnos);");
        return execArray;
    }

    public static
    String getRequestBodyContent() {
        // 返回请求体的内容
        return "{\n" +
                "    \"datas\": [\n" +
                "        {\n" +
                "                \"businesscorpno\": \"{{clientno}}\",\n" +
                "                \"corpserialno\": \"{{$timestamp}}\",\n" +
                "                \"countrycode\":\"NZL\",\n" +
                "                \"engname\":\"2023-7-27 19:41:16hello test buyer03\",\n" +
                "                \"buyeraddr\":\"2023-7-27 19:41:19hello test buyer01 addr\",\n" +
                "                \"ifgov\":\"0\",\n" +
                "                \"ifquotedcompany\":\"0\"\n" +
                "            }\n" +
                "                ],\n" +
                "    \"imethod\": \"doBprBuyerCodeApply\"\n" +
                "}";
    }

    public static
    void writeJsonToFile(String jsonString, String fileName) {
        // 写入 JSON 到文件
        // 省略文件写入部分，您可以使用您喜欢的文件写入方式，例如 FileWriter
        System.out.println("JSON written to file: " + fileName);
        try (FileWriter fileWriter = new FileWriter(fileName)) {
            fileWriter.write(jsonString);
            System.out.println("JSON written to file: " + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
